package com.sojson.project.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.constant.Constant;
import com.sojson.enums.core.ECouponStatus;
import com.sojson.project.controller.param.verify.CouponParamVerify;
import com.sojson.project.entity.po.Coupon;
import com.sojson.project.entity.vo.CouponVo;
import com.sojson.project.service.CouponService;
import com.sojson.result.Result;
import com.sojson.util.DateUtil;
import com.sojson.util.ExpUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 优惠券控制器
 * 
 * @author liu
 * @date 2021-03-24
 */
@Api(tags = "优惠券管理")
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    /**
     * 单条插入
     * 
     * @param coupon
     * @return
     * @throws IOException 
     * @throws SQLException
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/coupon/insert')")
    @ApiOperation("给优惠券添加一条数据")
    public Result<Integer> insert(@RequestBody Coupon coupon) throws IOException {
        // 校验参数
        Result<Integer> paramResult = CouponParamVerify.insert(coupon);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        coupon.insertInit();
        return Result.success(couponService.insert(coupon));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param coupon
     * @return
     * @throws SQLException
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/coupon/updateById')")
    @ApiOperation("通过id更新优惠券数据(局部更新)")
    public Result<Integer> updateById(@RequestBody Coupon coupon) {
        // 校验参数
        Result<Integer> paramResult = CouponParamVerify.updateById(coupon);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        coupon.updateInit();
        coupon.setUpdateNull();
        return Result.success(couponService.updateById(coupon));
    }

    /**
     * 添加发放数量
     * 
     * @param coupon
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/addNumber")
    @PreAuthorize("@ss.hasPermi('/coupon/addNumber')")
    @ApiOperation("通过id添加优惠券数量")
    public Result<Integer> addNumber(@RequestBody Coupon coupon) {
        // 校验参数
        Result<Integer> paramResult = CouponParamVerify.addNumber(coupon);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Coupon cou = new Coupon();
        cou.updateInit();
        cou.setId(coupon.getId());
        cou.setNumber(coupon.getNumber());
        return Result.success(couponService.addNumber(cou));
    }

    /**
     * 修改优惠券状态
     * 
     * @param coupon
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updateStatus")
    @PreAuthorize("@ss.hasPermi('/coupon/updateStatus')")
    @ApiOperation("通过id修改优惠券状态")
    public Result<Integer> updateStatus(@RequestBody Coupon coupon) {
        // 校验参数
        Result<Integer> paramResult = CouponParamVerify.updateStatus(coupon);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Coupon cou = new Coupon();
        cou.updateInit();
        cou.setId(coupon.getId());
        cou.setStatus(coupon.getStatus());
        return Result.success(couponService.updateStatus(cou));
    }

    /**
     * 批量插入
     * 
     * @param coupon
     * @return
     */
    @PostMapping("/insertList")
    @PreAuthorize("@ss.hasPermi('/coupon/insertList')")
    @ApiOperation("给优惠券添加多条数据")
    public Result<Integer> insertList(@RequestBody Coupon coupon) {
        return Result.success(couponService.insertList(coupon));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param coupon
     * @return
     */
    @DeleteMapping("/deleteByDto")
    @PreAuthorize("@ss.hasPermi('/coupon/deleteByDto')")
    @ApiOperation("通过对象批量删除优惠券数据(物理删除)")
    public Result<Integer> deleteByDto(@RequestBody Coupon coupon) {
        return Result.success(couponService.deleteByDto(coupon));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @GetMapping("/countAll")
    @PreAuthorize("@ss.hasPermi('/coupon/countAll')")
    @ApiOperation("统计优惠券全部数据(包含逻辑删除的)")
    public Result<Integer> countAll() {
        return Result.success(couponService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @GetMapping("/listAll")
    @PreAuthorize("@ss.hasPermi('/coupon/listAll')")
    @ApiOperation("查询优惠券所有数据(包含逻辑删除的)")
    public Result<List<Coupon>> listAll() {
        return Result.success(couponService.listAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param coupon
     * @return
     */
    @GetMapping("/countByDto")
    @PreAuthorize("@ss.hasPermi('/coupon/countByDto')")
    @ApiOperation("根据对象统计优惠券数据")
    public Result<Integer> countByDto(Coupon coupon) {
        return Result.success(couponService.countByDto(coupon));
    }

    /**
     * 根据对象查询
     * 
     * @param coupon
     * @return
     */
    @GetMapping("/listByDto")
    @PreAuthorize("@ss.hasPermi('/coupon/listByDto')")
    @ApiOperation("根据对象查询优惠券数据")
    public Result<List<Coupon>> listByDto(Coupon coupon) {
        return Result.success(couponService.listByDto(coupon));
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@ss.hasPermi('/coupon/deleteById/{id}')")
    @ApiOperation("通过id删除优惠券数据(物理删除)")
    public Result<Integer> deleteById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(couponService.deleteById(id));
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/coupon/deleteByIds')")
    @ApiOperation("通过id数组删除优惠券数据(物理删除)")
    public Result<Integer> deleteByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(couponService.deleteByIds(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param coupon
     * @return
     */
    @PutMapping("/updateAllById")
    @PreAuthorize("@ss.hasPermi('/coupon/updateAllById')")
    @ApiOperation("通过id更新优惠券数据(全部更新)")
    public Result<Integer> updateAllById(@RequestBody Coupon coupon) {
        return Result.success(couponService.updateAllById(coupon));
    }

    /**
     * 通过id列更新(全部更新)(更新内容相同)
     * 
     * @param coupon
     * @return
     */
    @PutMapping("/updateAllByIdList")
    @PreAuthorize("@ss.hasPermi('/coupon/updateAllByIdList')")
    @ApiOperation("通过id列更新优惠券数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByIdList(@RequestBody Coupon coupon) {
        return Result.success(couponService.updateAllByIdList(coupon));
    }

    /**
     * 通过id列更新(全部更新)(更新内容不相同)
     * 
     * @param coupon
     * @return
     */
    @PutMapping("/updateDtoListAllById")
    @PreAuthorize("@ss.hasPermi('/coupon/updateDtoListAllById')")
    @ApiOperation("通过id列更新优惠券数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllById(@RequestBody Coupon coupon) {
        return Result.success(couponService.updateDtoListAllById(coupon));
    }

    /**
     * 通过id列更新(局部更新)(更新内容相同)
     * 
     * @param coupon
     * @return
     */
    @PutMapping("/updateByIdList")
    @PreAuthorize("@ss.hasPermi('/coupon/updateByIdList')")
    @ApiOperation("通过id列更新优惠券数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByIdList(@RequestBody Coupon coupon) {
        return Result.success(couponService.updateByIdList(coupon));
    }

    /**
     * 通过id列更新(局部更新)(更新内容不相同)
     * 
     * @param coupon
     * @return
     */
    @PutMapping("/updateDtoListById")
    @PreAuthorize("@ss.hasPermi('/coupon/updateDtoListById')")
    @ApiOperation("通过id列更新优惠券数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListById(@RequestBody Coupon coupon) {
        return Result.success(couponService.updateDtoListById(coupon));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @PreAuthorize("@ss.hasPermi('/coupon/findById/{id}')")
    @ApiOperation("通过id查询优惠券数据")
    public Result<Coupon> findById(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(couponService.findById(id));
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @GetMapping("/findByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/coupon/findByIds/{ids}')")
    @ApiOperation("通过id数组查询优惠券数据")
    public Result<List<Coupon>> findByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(couponService.findByIds(ids));
    }

    /**
     * 通过对象查询id列
     * 
     * @param coupon
     * @return
     */
    @GetMapping("/findIdByDto")
    @PreAuthorize("@ss.hasPermi('/coupon/findIdByDto')")
    @ApiOperation("通过对象查询优惠券id列")
    public Result<List<Integer>> findIdByDto(Coupon coupon) {
        return Result.success(couponService.findIdByDto(coupon));
    }

    /**
     * 通过no删除(物理删除)
     * 
     * @param no
     * @return
     */
    @DeleteMapping("/deleteByNo/{no}")
    @PreAuthorize("@ss.hasPermi('/coupon/deleteByNo/{no}')")
    @ApiOperation("通过no删除优惠券数据(物理删除)")
    public Result<Integer> deleteByNo(@PathVariable String no) {
        if (no == null) {
            return Result.success();
        }

        return Result.success(couponService.deleteByNo(no));
    }

    /**
     * 通过no数组删除(物理删除)
     * 
     * @param nos
     * @return
     */
    @DeleteMapping("/deleteByNos/{nos}")
    @PreAuthorize("@ss.hasPermi('/coupon/deleteByNos/{nos}')")
    @ApiOperation("通过no数组删除优惠券数据(物理删除)")
    public Result<Integer> deleteByNos(@PathVariable String[] nos) {
        if (nos.length == 0) {
            return Result.success();
        }

        return Result.success(couponService.deleteByNos(nos));
    }

    /**
     * 通过no更新(全部更新)
     * 
     * @param coupon
     * @return
     */
    @PutMapping("/updateAllByNo")
    @PreAuthorize("@ss.hasPermi('/coupon/updateAllByNo')")
    @ApiOperation("通过no更新优惠券数据(全部更新)")
    public Result<Integer> updateAllByNo(@RequestBody Coupon coupon) {
        return Result.success(couponService.updateAllByNo(coupon));
    }

    /**
     * 通过no列更新(全部更新)(更新内容相同)
     * 
     * @param coupon
     * @return
     */
    @PutMapping("/updateAllByNoList")
    @PreAuthorize("@ss.hasPermi('/coupon/updateAllByNoList')")
    @ApiOperation("通过no列更新优惠券数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByNoList(@RequestBody Coupon coupon) {
        return Result.success(couponService.updateAllByNoList(coupon));
    }

    /**
     * 通过no列更新(全部更新)(更新内容不相同)
     * 
     * @param coupons
     * @return
     */
    @PutMapping("/updateDtoListAllByNo")
    @PreAuthorize("@ss.hasPermi('/coupon/updateDtoListAllByNo')")
    @ApiOperation("通过no列更新优惠券数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllByNo(@RequestBody Coupon coupons) {
        return Result.success(couponService.updateDtoListAllByNo(coupons));
    }

    /**
     * 通过no更新(局部更新)
     * 
     * @param coupon
     * @return
     */
    @PutMapping("/updateByNo")
    @PreAuthorize("@ss.hasPermi('/coupon/updateByNo')")
    @ApiOperation("通过no更新优惠券数据(局部更新)")
    public Result<Integer> updateByNo(@RequestBody Coupon coupon) {
        return Result.success(couponService.updateByNo(coupon));
    }

    /**
     * 通过no列更新(局部更新)(更新内容相同)
     * 
     * @param coupon
     * @return
     */
    @PutMapping("/updateByNoList")
    @PreAuthorize("@ss.hasPermi('/coupon/updateByNoList')")
    @ApiOperation("通过no列更新优惠券数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByNoList(@RequestBody Coupon coupon) {
        return Result.success(couponService.updateByNoList(coupon));
    }

    /**
     * 通过no列更新(局部更新)(更新内容不相同)
     * 
     * @param coupons
     * @return
     */
    @PutMapping("/updateDtoListByNo")
    @PreAuthorize("@ss.hasPermi('/coupon/updateDtoListByNo')")
    @ApiOperation("通过no列更新优惠券数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListByNo(@RequestBody Coupon coupons) {
        return Result.success(couponService.updateDtoListByNo(coupons));
    }

    /**
     * 通过no查询
     * 
     * @param no
     * @return
     */
    @GetMapping("/findByNo/{no}")
    @PreAuthorize("@ss.hasPermi('/coupon/findByNo/{no}')")
    @ApiOperation("通过no查询优惠券数据")
    public Result<Coupon> findByNo(@PathVariable String no) {
        if (no == null) {
            return Result.success();
        }

        return Result.success(couponService.findByNo(no));
    }

    /**
     * 通过no数组查询
     * 
     * @param nos
     * @return
     */
    @GetMapping("/findByNos/{nos}")
    @PreAuthorize("@ss.hasPermi('/coupon/findByNos/{nos}')")
    @ApiOperation("通过no数组查询优惠券数据")
    public Result<List<Coupon>> findByNos(@PathVariable String[] nos) {
        if (nos.length == 0) {
            return Result.success();
        }

        return Result.success(couponService.findByNos(nos));
    }

    /**
     * 通过对象查询no列
     * 
     * @param coupon
     * @return
     */
    @GetMapping("/findNoByDto")
    @PreAuthorize("@ss.hasPermi('/coupon/findNoByDto')")
    @ApiOperation("通过对象查询优惠券no列")
    public Result<List<String>> findNoByDto(Coupon coupon) {
        return Result.success(couponService.findNoByDto(coupon));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/coupon/clear')")
    @ApiOperation("清空优惠券数据(物理清空)")
    public Result<Integer> clear() {
        return Result.success(couponService.clear());
    }

    /**
     * 查询可领取的优惠券列表
     * 
     * @param coupon
     * @return
     */
    @GetMapping("/getCanReceive")
    @PreAuthorize("@ss.hasPermi('/coupon/getCanReceive')")
    @ApiOperation("查询可领取的优惠券列表")
    public Result<List<CouponVo>> getCanReceive(CouponVo coupon) {
        return Result.success(couponService.getCanReceive(coupon));
    }

    /**
     * 查询可领取的优惠券列表
     * 
     * @param coupon
     * @return
     * @throws IOException 
     */
    @GetMapping("/getCanReceiveApp")
    @PreAuthorize("@ss.login()")
    @ApiOperation("查询可领取的优惠券列表")
    public Result<List<CouponVo>> getCanReceiveApp(CouponVo coupon) throws IOException {
        coupon.setThisTime(DateUtil.nowDate());
        coupon.setStatus(ECouponStatus.YES_LAUNCH.getCode());
        coupon.setUseUserNoVo(TokenUtil.getUserId());
        return Result.success(couponService.getCanReceive(coupon));
    }

    /**
     * 删除优惠券
     * 
     * @param coupon
     * @return
     */
    @DeleteMapping("/delete/{ids}")
    @PreAuthorize("@ss.hasPermi('/coupon/delete')")
    @ApiOperation("删除优惠券")
    public Result<Integer> delete(@PathVariable Integer[] ids) {
        if (StringUtil.isBlankObject(ids) || ids.length < 1) {
            ExpUtil.throwEx("优惠券ID不能为空！");
        }

        CouponVo coupon = new CouponVo();
        coupon.setIds(Arrays.asList(ids));
        coupon.setThisTime(new Date(DateUtil.nowLong() - Constant.COUPON_SELECT_ABORT_TIME_MILLIS));
        return Result.success(couponService.delete(coupon));
    }

}